﻿<template>
	<el-dialog :title="!dataForm.id ? '新建' : '编辑'" :close-on-click-modal="false" :visible.sync="visible"
		class="SHZY-dialog SHZY-dialog_center" lock-scroll width="600px">
		<el-row :gutter="15" class="">
			<el-form ref="elForm" :model="dataForm" size="small" label-width="100px" label-position="right" :rules="rules">
				<el-col :span="24">
					<el-form-item label="物料号" prop="matnr">
						<SHZY-select :list="matnrCodeOptions" placeholder="请选择物料号"
							v-model:bindValue="dataForm.matnr" isConcat label="name" value="code"></SHZY-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="采购类型" prop="orderType">
						<el-select v-model='dataForm.orderType' placeholder='请选择采购类型' clearable required
							:style='{ "width": "100%" }'>
							<el-option v-for="(item, index) in orderTypeOptions" :key="index" :label="item.fullName"
								:value="item.enCode"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="供应商" prop="supplierNo">
						<SHZY-select :list="supplierNoOptions" placeholder="请选择供应商"
							v-model:bindValue="dataForm.supplierNo" isConcat label="name" value="code"></SHZY-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="跳检规则" prop="ruleCode">
						<SHZY-select :list="roleCodeOptions" placeholder="请选择跳检规则"
							v-model:bindValue="dataForm.ruleCode" isConcat label="desc" value="code"></SHZY-select>
					</el-form-item>
				</el-col>
				<el-col :span="24">
					<el-form-item label="有效标志" prop="enabledMark">
						<el-switch v-model='dataForm.enabledMark' :active-value='1' :inactive-value='0'>
						</el-switch>
					</el-form-item>
				</el-col>
			</el-form>
		</el-row>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" @click="dataFormSubmit()" :loading="btnLoading">确 定</el-button>
		</span>
	</el-dialog>
</template>
<script>
import { getExemptionruleInfo, createExemptionrule, updateExemptionrule } from '@/api/q1/basedata/exemptionRule'
import { GetSupplierAll } from '@/api/q1/basedata/supplier'
import { getMaterialAll } from '@/api/q1/basedata/material'
import { getInspectionroleAll } from '@/api/q1/basedata/inspectionRole'
export default {
	components: {},
	props: [],
	data() {
		return {
			btnLoading: false,
			loading: false,
			visible: false,
			dataForm: {
				id: '',
				matnr: undefined,
				orderType: undefined,
				supplierNo: undefined,
				ruleCode: undefined,
				enabledMark: 1,
			},
			rules: {
				matnr: [
					{
						required: true,
						message: '请选择物料号',
						trigger: '"change"'
					},
				],
				orderType: [
					{
						required: true,
						message: '请选择采购类型',
						trigger: '"change"'
					},
				],
				supplierNo: [
					{
						required: true,
						message: '请选择供应商',
						trigger: '"change"'
					},
				],
				ruleCode: [
					{
						required: true,
						message: '请选择跳检规则',
						trigger: '"change"'
					},
				],
			},
			matnrCodeOptions: [],
			orderTypeOptions: [],
			supplierNoOptions: [],
			roleCodeOptions: [],
		}
	},
	computed: {},
	watch: {},
	created() {
		this.getDictionaryData();
	},
	mounted() {
	},
	methods: {
		getDictionaryData() {
				this.$store.dispatch('base/getDictionaryData', { sort: 'BatchType' }).then((res) => {
					this.orderTypeOptions = res
				})
				getMaterialAll().then(res => {
                    this.matnrCodeOptions = res.data.list
                })
				GetSupplierAll().then(res => {
                    this.supplierNoOptions = res.data.list
                })
				getInspectionroleAll().then(res => {
                    this.roleCodeOptions = res.data.list
                })
			},
		goBack() {
			this.$emit('refresh')
		},
		init(id) {
			this.dataForm.id = id || 0;
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['elForm'].resetFields();
				if (this.dataForm.id) {
					getExemptionruleInfo(this.dataForm.id).then(res => {
						this.dataForm = res.data;
					})
				}
			});
			//this.$store.commit('generator/UPDATE_RELATION_DATA', {})
		},
		dataFormSubmit() {
			this.$refs['elForm'].validate((valid) => {
				if (valid) {
					this.btnLoading = true;

					const formMethod = !this.dataForm.id ? createExemptionrule : updateExemptionrule;

					formMethod(this.dataForm).then((res) => {
						this.$message({
							message: res.msg,
							type: 'success',
							duration: 1000,
							onClose: () => {
								this.btnLoading = false;
								this.visible = false,
									this.$emit('refresh', true)
							}
						})
					}).catch((res) => {
						this.btnLoading = false;
					})

				}
			})
		},
	}
}
</script>
